package SA.student;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class SAstudentActivity extends Activity {
	public String serveridpw1;
	public String studentID;
	public String studentPW;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		ImageView loginBtn = (ImageView) findViewById(R.id.login_btn);
		loginBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				EditText eTextID = (EditText) findViewById(R.id.eTextID);
				EditText eTextPW = (EditText) findViewById(R.id.eTextPW);

				studentID = eTextID.getText().toString();
				studentPW = eTextPW.getText().toString();
				String clientidpw = studentID + studentPW;
				login(clientidpw);
			}
		});
	}

	void login(String clientidpw) {

		Parse ps = new Parse();
		ps.run();//로그인 접속
		//if (studentID.equals("1111") && studentPW.equals("0000")) {
			if (clientidpw.equals(serveridpw1)) {

				Intent intent = new Intent(this, MainActivity.class);
				intent.putExtra("ID",studentID);
				startActivity(intent);

			} else {
				new AlertDialog.Builder(SAstudentActivity.this).setTitle("로그인 실패")
						.setMessage("아이디 비번 확인").setCancelable(false)
						.setNegativeButton("다시 로그인", null).show();
			}
		/*} else {
			new AlertDialog.Builder(SAstudentActivity.this).setTitle("濡쒓렇��)
					.setMessage("濡쒓렇�몄떎��").setCancelable(false)
					.setNegativeButton("�뺤씤", null).show();
		}*/
	}
	
	private class Parse implements Runnable {
		public void run() {
			// TODO Auto-generated method stub
			try {
				String addr = "Http://220.67.128.8/search.php?teacherID="
						+ studentID + "&teacherPW=" + studentPW;

				URL url = new URL(addr);

				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();

				InputStream is = null;
				ByteArrayOutputStream baos = null;
				Log.e("login 응답 : ", "접속0");
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					Log.e("login 응답 : ", "접속1");
					is = conn.getInputStream();
					Log.e("login 응답 : ", "접속2");
					baos = new ByteArrayOutputStream();
					Log.e("login 응답 : ", "접속3");
					byte[] byteBuffer = new byte[1024];
					byte[] byteData = null;
					int nLength = 0;
					Log.e("login 응답 : ", "접속4");
					while ((nLength = is.read(byteBuffer)) > 0) {
						baos.write(byteBuffer, 0, nLength);
					}
					Log.e("login 응답 : ", "접속5");
					byteData = baos.toByteArray();
					Log.e("login 응답 : ", "접속6");
					// String response = new String(byteData, "euc-kr");
					String response = new String(byteData);
					Log.e("login 응답 : ", "접속7");
					Log.e("login 응답 : ", response);
					serveridpw1 = response;
					Log.e("login 응답 : ", serveridpw1);
					if (response == null || response.equals("")) {
						throw new Exception("Response is NULL!");
					}
				} else {
					Log.e("login 응답 : ", "접속 안됨");
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}

}